﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;

namespace Faust.Andrew.MTG
{
	/// <summary>
	/// Interaction logic for DeckStatistics.xaml
	/// </summary>
	public partial class DeckStatistics : UserControl
	{
        private int _cardCount;
        protected Dictionary<string, int> _cardTypes;
        protected Dictionary<string, int> _cardColors;

        private Deck _deck;
        public Deck Deck
        {
            get { return _deck; }
            set
            {
                //If it's already pointed at a deck we need to remove the event handler
                if (_deck != null)
                {
                    _deck.PropertyChanged -= _deck_PropertyChanged;
                }

                _deck = value;

                //If it was set to a deck we need to set the event handler
                if (_deck != null)
                {
                    _deck.PropertyChanged += _deck_PropertyChanged;
                }

                //Update the UI
                ResetStatistics();
            }
        }

        private void SetLandCountStatistic(DeckCard deckCard)
        {
            string cardName = deckCard.Card.Name;

            if (_cardTypes.ContainsKey(cardName))
            {
                _cardTypes[cardName] += deckCard.Quantity;
            }
            else
            {
                _cardTypes.Add(cardName, deckCard.Quantity);
            }
        }

        private void SetColorCountStatistic(DeckCard deckCard)
        {
            string color = deckCard.Card.Color;
            if (_cardColors.ContainsKey(color))
            {
                _cardColors[color] += deckCard.Quantity;
            }
            else
            {
                _cardColors.Add(color, deckCard.Quantity);
            }
        }

        private void SetCardTypeStatistic(DeckCard deckCard)
        {
            string finalCardType = "Other";
            string cardType = deckCard.Card.CardTypes;
            if (IsType(cardType, "Creature"))
            {
                finalCardType = "Creature";
            }
            else if (IsType(cardType, "Instant"))
            {
                finalCardType = "Instant";
            }
            else if (IsType(cardType, "Sorcery"))
            {
                finalCardType = "Sorcery";
            }
            else if (IsType(cardType, "Artifact"))
            {
                finalCardType = "Artifact";
            }
            else if (IsType(cardType, "Enchantment"))
            {
                finalCardType = "Enchantment";
            }

            //Add the Card Type to the dictionary
            if (_cardTypes.ContainsKey(finalCardType))
            {
                _cardTypes[finalCardType] += deckCard.Quantity;
            }
            else
            {
                _cardTypes.Add(finalCardType, deckCard.Quantity);
            }
        }

        private void UpdateUI()
        {
            lblTotalCards.Content = _cardCount.ToString();

            cardColorSeries.DataContext = _cardColors;
            cardTypeSeries.DataContext = _cardTypes;
        }

        private void ResetStatistics()
        {
            Clear();
            if (_deck == null) { return; }

            _cardCount = 0;

            foreach (DeckCard deckCard in _deck.Cards)
            {
                _cardCount += deckCard.Quantity;
                if (IsBasicLand(deckCard.Card.Name))
                {
                    //We need to break out the land to separate "Types" based on color.
                    SetLandCountStatistic(deckCard);
                }
                else
                {
                    //Increment the color
                    SetColorCountStatistic(deckCard);

                    //Now we break out the statistics by card type
                    SetCardTypeStatistic(deckCard);
                }
            }

            //Now we need to update the visual elements
            UpdateUI();
        }

        private bool IsBasicLand(string cardName)
        {
            return (cardName == "Swamp" || cardName == "Plains" || cardName == "Mounting" || cardName == "Forest" || cardName == "Island");
        }

        private bool IsType(string cardType, string typeToCheck)
        {
            return cardType.Contains(typeToCheck);
        }

        private void Clear()
        {
            _cardTypes = new Dictionary<string, int>();
            _cardColors = new Dictionary<string, int>();
            lblTotalCards.Content = String.Empty;
        }

        void _deck_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ResetStatistics();
        }

		public DeckStatistics()
		{
			this.InitializeComponent();
            _cardTypes = new Dictionary<string, int>();
            _cardColors = new Dictionary<string, int>();
		}
	}
}